<%@page import="models.OrderProductModel"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Locale"%>
<%@page import="java.text.NumberFormat"%>
<%@page import="data.DealData"%>
<%@page import="java.util.ArrayList"%>
<%@page import="models.OrderModel"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%	
	List<OrderModel> list = (ArrayList<OrderModel>) request.getAttribute("orders");
	DealData dealData = new DealData();
	NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);   
	SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
	String totalFrom = (String)request.getAttribute("totalFrom");
	String totalTo = (String)request.getAttribute("totalTo");
	String strPeriod = (String)request.getAttribute("strPeriod");
	if(strPeriod==null||strPeriod.equals("")){
		strPeriod = "A";
	}
	String fromDate = (String)request.getAttribute("fromDate");
	String toDate = (String)request.getAttribute("toDate");
%>
<jsp:include page="/layouts/header.jsp">
	<jsp:param name="language" value="1" />	
</jsp:include>	
<link rel="stylesheet" href="http://code.jquery.com/ui/1.8.23/themes/base/jquery-ui.css" type="text/css" media="all" />
<script type="text/javascript" src="/js/jquery-ui-1.8.18.custom.min.js"></script>
<script type="text/javascript" src="/js/period_selector.js"></script>
<script>
$(document).ready(function(){
	var config = {};
	config = {
				changeMonth: true,
				duration: 'fast',
				changeYear: true,
				numberOfMonths: 1,
				selectOtherMonths: true,
				showOtherMonths: true,
				firstDay: 1,
				dayNames: ['CN', 'Thứ 2', 'Thứ 3', 'Thứ 4', 'Thứ 5', 'Thứ 6', 'Thứ 7'],
				monthNamesShort: ['Thg 1', 'Thg 2', 'Thg 3', 'Thg 4', 'Thg 5', 'Thg 6', 'Thg 7', 'Thg 8', 'Thg 9', 'Thg 10', 'Thg 11', 'Thg 12'],
				yearRange: '2010:2013',
				dateFormat: 'dd/mm/yy'
			};
	$(function() {
		$( "#f_date, #t_date" ).datepicker(config);
	});
	$("#period_selects").cePeriodSelector({
		from: "f_date",
		to: "t_date"
	});

	function fn_check_valid_search_date(){
		var f_date = document.getElementById('f_date').value;
		var t_date = document.getElementById('t_date').value;
					
		var array_f_date = f_date.split("/");
		var array_t_date = t_date.split("/");
		
		f_date = array_f_date[2] + '' + array_f_date[1] + '' + array_f_date[0];
		t_date = array_t_date[2] + '' + array_t_date[1] + '' + array_t_date[0];
							
		if(f_date != '' && t_date != '' && f_date>t_date){
			return '_end_date_cannot_less_than_start_date';
		}	
		return true;		
	}	
	$("#order-cancel").click(function(){
		var conf = confirm("Bạn có chắc chắn muốn hủy đơn hàng này ?");
		if(!conf){
			return false;
		}		
	});
});
</script>
        	<div id="content_inner">        	
            	<div id="productDetailInfo" class="clearafter">            		
                	<div class="left">                    	
                    	<div id="manageOrdersPage">
							<h1>Đơn hàng</h1>
                            <div class="searchOrders">
                            	<h3><a href="#">Tìm kiếm</a></h3>
                                <form method="post" action="/quan-ly-don-hang" class="formSearchOrders">
                                	<div class="clearafter">
                                    	<label>Tổng cộng (VNĐ):</label>
                                        <input type="text" name="totalFrom" value="<%=(totalFrom!=null&&!totalFrom.equals(""))?totalFrom:""%>"/>
                                         - 
                                        <input type="text" name="totalTo" value="<%=(totalTo!=null&&!totalTo.equals(""))?totalTo:""%>"/>
                                    </div>
                                    <div class="clearafter">                                    
                                    	<label>Thời gian:</label>
                                        <select name="period" id="period_selects">
											<option value="A" <%=(strPeriod.equals("A"))?"selected":"" %>>Tất cả</option>
											<optgroup label="=============">
											<option value="D" <%=(strPeriod.equals("D"))?"selected":"" %>>Hôm nay</option>
											<option value="W" <%=(strPeriod.equals("W"))?"selected":"" %>>Tuần này</option>
											<option value="M" <%=(strPeriod.equals("M"))?"selected":"" %>>Tháng này</option>
											<option value="Y" <%=(strPeriod.equals("Y"))?"selected":"" %>>Năm nay</option>
											</optgroup>
											<optgroup label="=============">
											<option value="LD" <%=(strPeriod.equals("LD"))?"selected":"" %>>Hôm qua</option>
											<option value="LW" <%=(strPeriod.equals("LW"))?"selected":"" %>>Tuần trước</option>
											<option value="LM" <%=(strPeriod.equals("LM"))?"selected":"" %>>Tháng trước</option>
											<option value="LY" <%=(strPeriod.equals("LY"))?"selected":"" %>>Năm ngoái</option>
											</optgroup>
											<optgroup label="=============">
											<option value="HH">24 giờ trước</option>
											<option value="HW">7 ngày trước</option>
											<option value="HM">30 ngày trước</option>
											</optgroup>
											<optgroup label="=============">
											<option value="C">Khác...</option>
											</optgroup>
										</select>
                                    </div>
                                    <div class="clearafter">
                                    	<label>Chọn ngày:</label>
                                        <input type="text" onchange="$('#period_selects').val('C');"  name="fromDate" id="f_date" value="<%=(fromDate!=null&&!fromDate.equals(""))?fromDate:""%>"/> 
                                        - 
                                        <input onchange="$('#period_selects').val('C');"  type="text" name="toDate" id="t_date" value="<%=(toDate!=null&&!toDate.equals(""))?toDate:""%>"/>                                        
                                    </div>
                                    <div class="clearafter">
                                    	<label>Mã đơn hàng: </label>
                                        <input type="text" name="orderId"/>
                                    </div>
                                    <button type="submit" class="button blue">Tìm kiếm</button>
                                </form>
                            </div>
                            <table cellpadding="0" cellspacing="0">
                            	<thead>
                                	<tr>
                                    	<th width="10%"><a href="#">ID</a></th>
                                        <th width="15%"><a href="#">Trạng thái</a></th>
                                        <th width="55%">Sản phẩm</th>
                                        <th width="10%"><a href="#">Ngày</a>&nbsp;<span class="down">↓</span></th>
                                        <th width="10%"><a href="#">Tổng cộng</a></th>
                                        <th width="10%"><a href="#">Thao tác</a></th>
                                    </tr>
                                </thead>
                                <tbody>
                                <%
                                	if(list!=null&&list.size()>0){
                                		for(OrderModel order: list){
                                %>
                                	<tr>
                                    	<td><a href="/don-hang/xem/<%=order.getOrderId()%>"><%=order.getOrderId() %></a></td>
                                        <td>
                                        	<%
												switch (order.getStatus()) {
													case 1: 
														%>Chưa xác nhận<%
														break;										
													case 2:
														%>Đã xác nhận<%
														break;
													case 3:
														%>Đang giao hàng<%
														break;
													case 4:
														%>Đã hoàn thành<%
														break;
													case 7:
														%>Kiểm tra lại<%
														break;
													case 5:
														%>Đã huỷ<%
														break;	
													case 8:
														%>Khoá<%
														break;	
													case 11:
														%>Đã thanh toán<%
														break;	
													default:
														%>Khác<%
														break;											
												}
												%>
                                        </td>
                                        <td>
                                        	<%
                                        		List<String> listStringProduct = order.getListOrderProduct();                                        
                                    			if(listStringProduct!=null&&listStringProduct.size()>0){
                                    				for(String stringProduct : listStringProduct){
                                    					OrderProductModel orderItem = new OrderProductModel();
                                    					orderItem.getValueFromString(stringProduct);
                                    	
                                        	%>                                        
											<p>[<%=orderItem.getQuantity() %>] - <%=orderItem.getName() %></p>                                        	
                                        	<%
                                    				}
                                   				}
                                        	%>
                                        </td>
                                        <td>
                                        	<a href="/don-hang/xem/<%=order.getOrderId()%>">
                                                <%=dateFormat.format(order.getOrderDate()) %>
                                            </a>
                                        </td>
                                        <td><%=nf.format(order.getTotalPrice()) %>&nbsp;VNĐ</td>
                                        <td>
                                        	<%
                                        		if(order.getStatus()==1){
                                        			%>
                                        			<a href="/don-hang/huy/<%=order.getOrderId()%>" id="order-cancel">
		                                                Hủy
		                                            </a>
                                        			<%
                                        		}
                                        	%>
                                        </td>
                                    </tr>
                                 <%
                                		}
                                	}else{
                                 %>  
                                 	<tr>
                                 		 <td colspan="7">
                                 		 	<p style="" align="center">Không có đơn hàng nào</p>
                                 		 </td>
                                 	</tr>     
                                 <%
                                	}
                                 %>                             
                                </tbody>
                            </table>
                        </div>                                               
                    </div>
                        
                   <div class="right rightSideBar">
                   		<jsp:include page="/elements/right_menu.jsp"/>
                   	</div>		        
                </div>                
            </div>   
<jsp:include page="/layouts/footer.jsp">
    <jsp:param name="language" value="1" />
</jsp:include>